package io.renren.modules.aic.controller;

import io.renren.common.annotation.SysLog;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.aic.entity.CtocTransCountEntity;
import io.renren.modules.aic.entity.MemberEntity;
import io.renren.modules.aic.service.CtocTransCountService;
import io.renren.modules.aic.service.MemberService;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;

/**
 * 用户订单交易统计表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-03-30 15:44:16
 */
@RestController
@RequestMapping("aic/ctoctranscount")
public class CtocTransCountController {
	@Autowired
	private CtocTransCountService ctocTransCountService;

	@Autowired
	private MemberService memberService;

	/**
	 * 列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/list")
	@RequiresPermissions("aic:ctoctranscount:list")
	public R list(@RequestParam Map<String, Object> params) {
		PageUtils page = ctocTransCountService.queryPage(params);
		List<CtocTransCountEntity> list = (List<CtocTransCountEntity>) page.getList();
		if (CollectionUtils.isNotEmpty(list)) {
			for (CtocTransCountEntity ctocTransCountEntity : list) {
				MemberEntity member = memberService.selectOne(new EntityWrapper<MemberEntity>().setSqlSelect("mobile", "realname").eq("id", ctocTransCountEntity.getMemberId()));
				ctocTransCountEntity.setMobile(member.getMobile());
				ctocTransCountEntity.setRealname(member.getRealname());
			}
		}
		return R.ok().put("page", page);
	}

	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("aic:ctoctranscount:info")
	public R info(@PathVariable("id") Long id) {
		CtocTransCountEntity ctocTransCount = ctocTransCountService.selectById(id);

		return R.ok().put("ctocTransCount", ctocTransCount);
	}

	/**
	 * 修改
	 */
	@SysLog("交易行为修改")
	@RequestMapping("/update")
	@RequiresPermissions("aic:ctoctranscount:update")
	public R update(@RequestBody CtocTransCountEntity ctocTransCount) {
		ValidatorUtils.validateEntity(ctocTransCount);

		ctocTransCount.setUpdateTime(new Date());
		ctocTransCountService.updateById(ctocTransCount);

		return R.ok();
	}

	/**
	 * 删除
	 */
	@SysLog("交易行为删除")
	@RequestMapping("/delete")
	@RequiresPermissions("aic:ctoctranscount:delete")
	public R delete(@RequestBody Long[] ids) {
		ctocTransCountService.deleteBatchIds(Arrays.asList(ids));

		return R.ok();
	}

}
